use std::path::Path;
use std::str::FromStr;

pub fn path_iter() {
    println!("--------path_iter--------");
    let path = Path::new("/Users/lhj/course/a.pdf");
    let mut iterator = path.iter();
    println!("{:?}", iterator.next());
    println!("{:?}", iterator.next());
    println!("{:?}", iterator.next());
    println!("{:?}", iterator.next());
    println!("{:?}", iterator.next());
    println!("{:?}", iterator.next());
}

pub fn string_iter() {
    println!("--------string_iter--------");
    let data = String::from("HelloWorld");
    let bytes = data.bytes();
    for b in bytes {
        println!("{}", b)
    }
    let chars = data.chars();
    for c in chars {
        println!("{}", c)
    }
}

pub fn string_map_filter() {
    println!("--------string_map_filter--------");
    let text = "   ponies  \n    giraffes\n iguanas    \n squid".to_string();
    let vs: Vec<&str> = text.lines()
        .map(str::trim)
        .filter(|s| !(*s).contains("igu"))
        // .filter(|s|!s.contains("igu"))  // 和上面一样的效果
        .collect();
    for v in vs {
        println!("{}", v);
    }

    let _ = text.lines().map(|line| println!("{}", line));
}

pub fn string_f64() {
    println!("--------string_f64--------");
    let text = "1\nfrond .25  289\n3.1415 estuary\n";
    for number in text.split_whitespace().filter_map(|w| f64::from_str(w).ok()) {
        println!("{:4.2}", number.sqrt());
    }
}

pub fn string_f642() {
    println!("--------string_f642--------");
    let text = "1\nfrond .25  289\n3.1415 estuary\n";
    text.split_whitespace()
        .map(|w| f64::from_str(w))
        .filter(|r| r.is_ok())
        // .map(|r| println!("{:4.2}", r.unwrap().sqrt())).
        .map(|r| r.unwrap())
        .for_each(|w| println!("{:4.2}", w.sqrt()));
}
